package com.ucap.weking.guangan.fragments.fragmentContent;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.ucap.weking.guangan.Activitys.NewContentActivity;
import com.ucap.weking.guangan.R;
import com.ucap.weking.guangan.adapter.LIstAdapter;
import com.ucap.weking.guangan.base.BaseFragment;
import com.ucap.weking.guangan.bean.ListTouLinItemBean;
import com.ucap.weking.guangan.dataUri.DataUri;
import com.ucap.weking.guangan.httpTools.HttpCallRequest;
import com.ucap.weking.guangan.modle.ItemModel;
import com.ucap.weking.guangan.views.LoadingListView;
import com.ucap.weking.guangan.views.MyToast;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Created by weking on 2017/10/17.
 * 广安要闻
 */

public class HomeContent1News extends BaseFragment {
    @InjectView(R.id.lv_content)
    LoadingListView lvContent;
    @InjectView(R.id.srlRefresh)
    SwipeRefreshLayout srlRefresh;
    @InjectView(R.id.tv_is_null)
    TextView tvIsNull;
    private LIstAdapter adapter;
    private int numow = 1;//页面开始角标
    private int num = 10;//页面结束角标
    private View headView;
    private BGABanner bgaBanner;
    private TextView tvTitle;

    @Override
    public void intiView() {
        LayoutInflater from = LayoutInflater.from(getActivity());
        View view = from.inflate(R.layout.frgament_content1_news, null);
        ButterKnife.inject(this, view);
        flContent.addView(view);
        headView = from.inflate(R.layout.new_top, null);
        bgaBanner = (BGABanner) headView.findViewById(R.id.banner_splash_pager);
        tvTitle = (TextView) headView.findViewById(R.id.tv_title);
    }

    @Override
    public void setData() {
        final ArrayList<ItemModel> mData = new ArrayList<>();
        adapter = new LIstAdapter(mData);
        lvContent.setAdapter(adapter);
        getData(mData, numow, num);
        srlRefresh.setColorSchemeResources(R.color.colorPrimary);
        srlRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                numow = 1;
                num = 10;
                mData.clear();
                getData(mData, numow, num);
            }
        });
        lvContent.setInterface(new LoadingListView.IloadListener() {
            @Override
            public void onLoad() {
                getData(mData, numow += 10, num += 10);
            }
        });
        lvContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int po = position - 1;
                if (po > -1 && po < mData.size()) {
                    ItemModel item = adapter.getItem(po);
                    startContentActivity(item.getContentUri());
                }
            }
        });
    }

    private void getData(final ArrayList<ItemModel> mData, final int startNum, final int endNum) {
        if (startNum == 1)
            srlRefresh.setRefreshing(true);
        HttpCallRequest.callRequest(DataUri.getGUANG_AN_YAO_WEN(startNum, endNum), new HttpCallRequest.OnCallListener() {
            @Override
            public void onRequest(final String body) {
                Log.i("number", "numow:  " + numow + " num: " + num);
                Log.i("number", "bpdy:  " + body);
                Log.i("number", "uri:  " + DataUri.getGUANG_AN_YAO_WEN(startNum, endNum));
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        ListTouLinItemBean listItemBean = gson.fromJson(body, ListTouLinItemBean.class);
                        if (listItemBean != null) {
                            ArrayList<ItemModel> data = new ArrayList<>();
                            List<ListTouLinItemBean.Rows2Bean> rows2 = listItemBean.getRows2();
                            Log.i("number", "dataSizeRows2:  " + rows2.size());
                            if (rows2 != null && rows2.size() > 0) {
                                for (ListTouLinItemBean.Rows2Bean bean2 : rows2) {
                                    ItemModel model = new ItemModel(bean2.getTITLE(), bean2.getTIME(), bean2.getID());
                                    model.setImageId(bean2.getPATHANDURL());
                                    model.setContentUri(bean2.getURL());
                                    data.add(model);
                                }
                                mData.addAll(data);
                                adapter.notifyDataSetChanged();
                                srlRefresh.setRefreshing(false);
                                lvContent.loadComplete();
                                tvIsNull.setVisibility(View.GONE);
                                Log.i("number", "dataSize:  " + mData.size());
                            } else {
                                MyToast.showTrue(getContext(), "已加载全部！");
                                srlRefresh.setRefreshing(false);
                                lvContent.loadComplete();
                                tvIsNull.setVisibility(View.GONE);
                                if (numow > 1) {
                                    numow -= 10;
                                    num -= 10;
                                }
                            }
                            //添加头条
                            List<ListTouLinItemBean.Rows1Bean> rows1 = listItemBean.getRows1();
                            addHead(rows1);
                        } else {
                            MyToast.showTrue(getContext(), "已加载全部！");
                            srlRefresh.setRefreshing(false);
                            lvContent.loadComplete();
                            tvIsNull.setVisibility(View.GONE);
                            if (numow > 1) {
                                numow -= 10;
                                num -= 10;
                            }
                        }
                    }
                });
            }

            @Override
            public void onErr(Exception e) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        srlRefresh.setRefreshing(false);
                        lvContent.loadComplete();
                        adapter.notifyDataSetChanged();
                        if (numow > 1) {
                            numow -= 10;
                            num -= 10;
                        }
                        if (mData.size() < 1) {
                            tvIsNull.setVisibility(View.VISIBLE);
                        } else {
                            tvIsNull.setVisibility(View.GONE);
                            MyToast.showTrue(getContext(), "连接超时，请检查网路连接！");
                        }
                        Log.i("number", "err   numow:  " + numow + " num: " + num);
                    }
                });

            }
        });
    }

    public void addHead(final List<ListTouLinItemBean.Rows1Bean> rows1) {
        if (rows1 != null && rows1.size() > 0) {
            lvContent.removeHeaderView(headView);
            ArrayList<View> views = new ArrayList<>();
            for (int i = 0; i < rows1.size(); i++) {
                ListTouLinItemBean.Rows1Bean rows1Bean = rows1.get(i);
                views.add(getView(rows1Bean.getPATHANDURL()));
            }
            bgaBanner.setData(views);
            tvTitle.setText(rows1.get(0).getTITLE());
            bgaBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    tvTitle.setText(rows1.get(position).getTITLE());
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            lvContent.addHeaderView(headView);
            bgaBanner.setOnItemClickListener(new BGABanner.OnItemClickListener() {
                @Override
                public void onBannerItemClick(BGABanner banner, View view, Object model, int position) {
                    startContentActivity(rows1.get(position).getURL());
                }
            });
        }
    }

    public void startContentActivity(String uri) {
        Intent intent = new Intent(getActivity(), NewContentActivity.class);
        intent.putExtra("title", "广安要闻");
        intent.putExtra("uri", uri);
        getActivity().startActivity(intent);

    }

    private ImageView getView(String uri) {
        ImageView imageView = new ImageView(getContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(this).load(uri).into(imageView);
        return imageView;
    }
}
